require "language/node"

class Esbuild < Formula
  desc "Extremely fast JavaScript bundler and minifier"
  homepage "https://esbuild.github.io/"
  url "https://registry.npmjs.org/esbuild/-/esbuild-0.14.9.tgz"
  sha256 "9357e3d1c325910669173256027ebc45498b2372bfb2e2b9c34360300adfccfb"
  license "MIT"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_monterey: "f98dc34b91fa3197d40bd65962fc3e596c2372cf55efe2225e2dac0c83fc257d"
    sha256 cellar: :any_skip_relocation, arm64_big_sur:  "f98dc34b91fa3197d40bd65962fc3e596c2372cf55efe2225e2dac0c83fc257d"
    sha256 cellar: :any_skip_relocation, monterey:       "6cac995792e2a27327f45857ec5e55b0acb23a424edb91daf0f61e838ec7f888"
    sha256 cellar: :any_skip_relocation, big_sur:        "6cac995792e2a27327f45857ec5e55b0acb23a424edb91daf0f61e838ec7f888"
    sha256 cellar: :any_skip_relocation, catalina:       "6cac995792e2a27327f45857ec5e55b0acb23a424edb91daf0f61e838ec7f888"
    sha256 cellar: :any_skip_relocation, x86_64_linux:   "3235acf1f447f2e79632463590055d6e1973b0fa1cfe6acafe7acd9905c09f08"
  end

  depends_on "node"

  def install
    system "npm", "install", *Language::Node.std_npm_install_args(libexec)
    bin.install_symlink Dir["#{libexec}/bin/*"]
  end

  test do
    (testpath/"app.jsx").write <<~EOS
      import * as React from 'react'
      import * as Server from 'react-dom/server'

      let Greet = () => <h1>Hello, world!</h1>
      console.log(Server.renderToString(<Greet />))
    EOS

    system Formula["node"].libexec/"bin/npm", "install", "react", "react-dom"
    system bin/"esbuild", "app.jsx", "--bundle", "--outfile=out.js"

    assert_equal "<h1 data-reactroot=\"\">Hello, world!</h1>\n", shell_output("node out.js")
  end
end
